package dao;

import model.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AdminDao {

    public static Admin login(Connection connection, Admin admin) throws Exception{
        Admin retAdmin = null;
        String select = "SELECT * FROM admin WHERE password = ? AND account = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(select);
        preparedStatement.setString(2, admin.getAccount());
        preparedStatement.setString(1, admin.getPassword());
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            retAdmin = new Admin();
            retAdmin.setPassword(resultSet.getString("password"));
            retAdmin.setAccount(resultSet.getString("account"));
        }
        return retAdmin;
    }

    public static Admin reSetPassword(Connection connection, Admin admin, String password) throws Exception {
        Admin retAdmin = new Admin();
        retAdmin.setPassword(password);
        retAdmin.setAccount(admin.getAccount());
        String sql = "UPDATE admin SET password = ? WHERE account = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, password);
        preparedStatement.setString(2, admin.getAccount());
        preparedStatement.executeUpdate();
        return retAdmin;
    }
}
